/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import service.Planet;
import service.PlanetFacadeREST;
import service.Tag;
import service.TagFacadeREST;

/**
 *
 * @author andrew
 */
public class deleteTagServlet extends HttpServlet {

    @EJB
    private PlanetFacadeREST planetManager;
    
    @EJB
    private TagFacadeREST tagManager;
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        // check if session is expired
        HttpSession session = request.getSession();// don't create if it doesn't exist
//        if(session != null && !session.isNew()) {} 
//        else {
//            response.sendRedirect("/home.jsp");
//        }
        
        String editingPlanetIdString = request.getParameter("editingPlanetId");
        String editingTagIdString = request.getParameter("tagId");
        
        //System.out.print("id pianeta: "+editingPlanetIdString);
        //System.out.print("tag: "+editingTagIdString);
        
        int editingPlanetId = Integer.parseInt(editingPlanetIdString);
        int editingTagId = Integer.parseInt(editingTagIdString);
        
        //System.out.print("id pianeta: "+editingPlanetId);
        
        Planet planet = planetManager.find(editingPlanetId);
        
        Collection<Tag> collTags = planet.getTagCollection();
        
        Iterator iterator = collTags.iterator();
            while (iterator.hasNext()) {     
                 Tag tag = (Tag) iterator.next();
                 if(tag.getIdTag() == editingTagId){
                     iterator.remove();
                 }
            } 
        
        planet.setTagCollection(collTags);
        
        planetManager.edit(planet);
        
        
        collTags = tagManager.findAll();
        
        
        
        //int idTagToDel = -1;
        
        iterator = collTags.iterator();
        while (iterator.hasNext()) {     
            Tag tag = (Tag) iterator.next();
            
            Collection<Planet> collPlanet = tag.getPlanetCollection();
            
            Iterator itPlanet = collPlanet.iterator();
            while(itPlanet.hasNext()){
               Planet p = (Planet) itPlanet.next();
               if(p.getIdPlanet() == editingPlanetId){
                   itPlanet.remove();
               }
            }
            tag.setPlanetCollection(collPlanet);
            tagManager.edit(tag);
        } 
        
        // elimino tag se non ha riferimenti ad alcun pianeta
        Tag t = tagManager.find(editingTagId);
        Collection<Planet> collPlanet = t.getPlanetCollection();
        if(collPlanet.isEmpty()){
            tagManager.remove(t);
        }
        
        
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
